//
//  YAYIShowAppointViewController.m
//  yayi-医生版
//
//  Created by Veer on 16/4/26.
//  Copyright © 2016年 Veer. All rights reserved.
//  预约列表

#import "YAYIShowAppointViewController.h"
#import "YAYIAppointDetailViewController.h"

#import "YAYIDropMenuView.h"

#import "YAYIMyAppointCell.h"
#import "YAYIMyAppointItem.h"

@interface YAYIShowAppointViewController () <UITableViewDelegate, UITableViewDataSource, YAYIDropMenuViewDelegate>


@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) YAYIDropMenuView *dropMenu;
// 没有数据显示提示
@property (nonatomic, weak) UILabel *noDataFlagLbl;


@property (nonatomic, strong) NSMutableArray *appointArray;

// 关于下拉列表的数据
@property (nonatomic, strong) NSMutableArray *allDropListArray;
@property (nonatomic, strong) NSMutableArray *section1Array;
@property (nonatomic, strong) NSMutableArray *section2Array;
@property (nonatomic, strong) NSMutableArray *section3Array;

// 下拉列表选择的参数
@property (nonatomic, copy) NSString *timeid;
@property (nonatomic, copy) NSString *bookstatusid;
@property (nonatomic, copy) NSString *hospitalid;

/** 分页相关 */
@property (nonatomic, assign) NSInteger curPage;
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation YAYIShowAppointViewController

- (NSMutableArray *)appointArray
{
    if (nil == _appointArray) {
        _appointArray = [NSMutableArray array];
    }
    return _appointArray;
}

- (NSMutableArray *)section1Array
{
    if (nil == _section1Array) {
        _section1Array = [NSMutableArray array];
        
        NSArray *array = @[@{@"name":@"全部", @"id":@"0"},
                           @{@"name":@"今日预约", @"id":@"1"},
                           @{@"name":@"明日预约", @"id":@"2"},
                           @{@"name":@"本周预约", @"id":@"3"},
                           @{@"name":@"本月预约", @"id":@"4"}
                           ];
        _section1Array = [array copy];
    }
    return _section1Array;
}

- (NSMutableArray *)section2Array
{
    if (nil == _section2Array) {
        _section2Array = [NSMutableArray array];
        
        NSArray *array = @[@{@"name":@"全部", @"id":@"00"},
                           @{@"name":@"已就诊", @"id":@"11"},
                           @{@"name":@"未就诊", @"id":@"10"},
                           @{@"name":@"已取消", @"id":@"20"}
                           ];
        _section2Array = [array copy];
    }
    return _section2Array;
}

- (NSMutableArray *)section3Array
{
    if (nil == _section3Array) {
        _section3Array = [NSMutableArray array];
    }
    return _section3Array;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.title = @"预约查看";
    
    [self setupTableView];
    [self setupListMenu];
    
    [self getAppoint];
//    [self getHospitalList];
    
    __block typeof(self) weakself = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakself getAppoint];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself getMoreAppoint];
    }];
//    [self.tableView.mj_footer setAutomaticallyHidden:YES];
}


- (void)back
{
    YAYILog(@"修改后的返回");
}


- (void)setupListMenu
{
    //列表展示的模型
    NSMutableArray *section1Datas = [NSMutableArray array];
    for (NSDictionary *dict in self.section1Array) {
        YAYIModel *model = [[YAYIModel alloc] init];
        model.name = dict[@"name"];
        model.id = dict[@"id"];
        [section1Datas addObject:model];
    }
    
    NSMutableArray *section2Datas = [NSMutableArray array];
    for (NSDictionary *dict in self.section2Array) {
        YAYIModel *model = [[YAYIModel alloc] init];
        model.name = dict[@"name"];
        model.id = dict[@"id"];
        [section2Datas addObject:model];
    }
    
    NSMutableArray *allDatas = [NSMutableArray array];
    [allDatas addObject:section1Datas];
    [allDatas addObject:section2Datas];
    [allDatas addObject:self.section3Array];
    
    self.allDropListArray = allDatas;

    //上边按钮展示的模型,此模型对text赋值即可.
    NSArray *titleArray = @[@"预约时间", @"预约状态", @"预约医院"];

    //若列表展示内容与按钮展示内容相同则showArr传nil即可。
    YAYIDropMenuView *dropMenu = [[YAYIDropMenuView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_width, 44) showOnView:self.view AllDataArr:allDatas showArr:[titleArray copy]];
    dropMenu.delegate = self;
    self.dropMenu = dropMenu;
    
}


- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(SCREEN_width));
        make.top.equalTo(@(44));
        make.bottom.equalTo(self.view);
        make.left.equalTo(self.view);
    }];
}

/**
 *  没有数据时显示
 */
- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"暂时没有数据";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView);
        make.centerY.equalTo(self.tableView).offset(-50);
    }];
}

#pragma mark - 获取数据
- (void)getAppoint
{
    // 显示hud
    [SVProgressHUD show];
    
    [self.appointArray removeAllObjects];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"timeid"] = self.timeid ? self.timeid : @"0";
    param[@"bookstatusid"] = self.bookstatusid ? self.bookstatusid : @"00";
    
    if (self.hospitalid) {
        param[@"hospitalid"] = self.hospitalid;
    }
    
    YAYILog(@"param:%@", param);
    
    [YAYINetWorking getWithURL:appointList_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
            }else{
                
                NSArray *data = dict[@"data"];
                
                NSString *timeStrTemp = nil;
                for (NSDictionary *obj in data) {
                    YAYIMyAppointItem *item = [YAYIMyAppointItem new];
                    [item setValuesForKeysWithDictionary:obj];
                    
                    NSString *timeStr = item.bookingtime;
                    YAYILog(@"time:%@  %@", timeStr, timeStrTemp);
                    if ([timeStr isEqualToString:timeStrTemp]) {
                        item.bookingtime = nil;
                    }
                    
                    [self.appointArray addObject:item];
                    timeStrTemp = timeStr;
                }
                
                self.curPage = 1;
                
                [self.tableView.mj_header endRefreshing];
                if (self.appointArray.count < 10) {
                    
                    [self.tableView.mj_footer endRefreshing];
                    self.tableView.mj_footer.hidden = YES;
                }else{
                    [self.tableView.mj_footer setState:MJRefreshStateIdle];
                    self.tableView.mj_footer.hidden = NO;
                }
                
                [self.tableView reloadData];
            }
            
            
            if (0 == self.appointArray.count) {
                [self showNoDataFlag:YES];
                
            }else{
                [self showNoDataFlag:NO];
            }
        }else{
        
            YAYILog(@"erro:%@", error);
        }
        
        // 取消显示hud
        [SVProgressHUD dismiss];
    }];
    
}


- (void)getMoreAppoint
{
    // 显示hud
    self.curPage++;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"timeid"] = self.timeid ? self.timeid : @"0";
    param[@"bookstatusid"] = self.bookstatusid ? self.bookstatusid : @"00";
    param[@"currentPage"] = @(self.curPage);
    
    if (self.hospitalid) {
        param[@"hospitalid"] = self.hospitalid;
    }
    
    YAYILog(@"param:%@", param);
    
    [YAYINetWorking getWithURL:appointList_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
            }else{
                
                NSArray *data = dict[@"data"];
                
                NSString *timeStrTemp = nil;
                for (NSDictionary *obj in data) {
                    YAYIMyAppointItem *item = [YAYIMyAppointItem new];
                    [item setValuesForKeysWithDictionary:obj];
                    
                    NSString *timeStr = item.bookingtime;
                    YAYILog(@"time:%@  %@", timeStr, timeStrTemp);
                    if ([timeStr isEqualToString:timeStrTemp]) {
                        item.bookingtime = nil;
                    }
                    
                    [self.appointArray addObject:item];
                    timeStrTemp = timeStr;
                }
                
                if (data.count < 10) {
                    [self.tableView.mj_footer endRefreshing];
                    self.tableView.mj_footer.hidden = YES;
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                
                [self.tableView reloadData];
            }
            
            
            if (0 == self.appointArray.count) {
                [self showNoDataFlag:YES];
                
            }else{
                [self showNoDataFlag:NO];
            }
        }else{
            
            YAYILog(@"erro:%@", error);
        }
        
        // 取消显示hud
        [SVProgressHUD dismiss];
    }];
    
}


- (void)getHospitalList
{
    if (self.section3Array.count > 0) {
        return;
    }
    
    [YAYINetWorking getWithURL:myHospital_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回:%@", dict);
            
            if (dict != nil) {
                
                if ([dict[@"data"] isEqual:NULL]) {
                    
                    return ;
                }else{
                    
                    NSArray *data = dict[@"data"];
                    for (NSDictionary *obj in data) {
                        YAYIModel *model = [[YAYIModel alloc] init];
                        model.name = obj[@"name"];
                        model.id = obj[@"id"];
                        [self.section3Array addObject:model];
                        
                    }
                    
                    [self.dropMenu refreshData:self.allDropListArray];
                }
                
            }else{
                YAYILog(@"网络异常");
            }
        }

    }];
}

#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.appointArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YAYIMyAppointCell *cell = [YAYIMyAppointCell cellWith:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.item = self.appointArray[indexPath.row];
    cell.indexPath = indexPath;
    
//    __block typeof(self) weakself = self;
//    cell.retreatOperation = ^(NSIndexPath *indexPath, UIButton *btn){
//        YAYILog(@"indexPath:%ld", indexPath.row);
//        
//        [weakself cancelAppointClick:indexPath button:btn];
//    };
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIMyAppointItem *item = self.appointArray[indexPath.row];
    if (item.bookingtime) {
        return 195 + 44;
    }
    return 195 + 13;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YAYIMyAppointItem *item = self.appointArray[indexPath.row];
    YAYIAppointDetailViewController *detailCtl = [[YAYIAppointDetailViewController alloc] init];
    detailCtl.appointID = [NSString stringWithFormat:@"%ld", item.bookingid];
    
    [self.navigationController pushViewController:detailCtl animated:YES];
}

#pragma mark YAYIDropMenuView代理
- (void)selectColum:(NSInteger)colum
{
    YAYILog(@"选择了低%ld列", colum);
    if (2 == colum) {
        [self getHospitalList];
    }
}

- (void)selectColum:(NSInteger)colum Row:(NSInteger)row Model:(YAYIModel *)model
{
    
    if (0 == colum) {
        self.timeid = model.id;
    }else if (1 == colum){
        self.bookstatusid = model.id;
    }else if (2 == colum){
        self.hospitalid = model.id;
    }
    [self getAppoint];
}

@end
